package com.example.vv.model.project;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.adapter.RecyclerViewHolder;
import com.example.vv.fragmentWork.entity.TreeClassEntity;
import com.example.vv.fragmentWork.mvp.MvpFragment;
import com.example.vv.fragmentWork.view.CustomerTitleBar;
import com.example.vv.model.project.base.contract.ChatContract;
import com.example.vv.model.project.base.presenter.ChatPresenter;
import com.example.vv.model.projectItem.PIFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;


public class ChatFragment extends MvpFragment<ChatContract.View, ChatPresenter> implements ChatContract.View {


    List<String> stringList;
    int index = -1;
    @BindView(R.id.title)
    CustomerTitleBar title;
    @BindView(R.id.chat_recycler)
    RecyclerView chatRecycler;
    @BindView(R.id.chat_fragment)
    FrameLayout chatFragment;


    @Override
    public int initLayout() {
        return R.layout.fragment_chat;
    }

    @Override
    public ChatPresenter initPresenter() {
        return new ChatPresenter();
    }

    @Override
    public void initView() {
        title.getLeft_img().setVisibility(View.INVISIBLE);
    }

    @Override
    public void lazyLoad() {
        presenter.setClassData();
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleBar() {
        return null;
    }

    @Override
    public Context getMContext() {
        return getActivity();
    }


    @Override
    public void getClassData(List<TreeClassEntity> classEntities) {
        stringList = new ArrayList<String>();
        for (int i = 0; i < classEntities.size(); i++) {
            stringList.add(classEntities.get(i).getName());
        }
        PIFragment fragment = new PIFragment(classEntities.get(0).getId());
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.chat_fragment, fragment);
        transaction.commit();

        BaseRecyclerViewAdapter<TreeClassEntity> adapter = new BaseRecyclerViewAdapter<TreeClassEntity>(getActivity(), classEntities, R.layout.item_project) {
            @SuppressLint("ResourceType")
            @Override
            protected void onBindData(RecyclerViewHolder holder, TreeClassEntity bean, int position) {
                TextView title = (TextView) holder.getView(R.id.tv_class);
                title.setText(bean.getName());
                if (position == index) {
                    title.setBackgroundResource(R.drawable.bg_project);
                    title.setTextColor(Color.parseColor("#ffff0505"));
                } else {
                    title.setBackgroundResource(R.drawable.bg_project_select);
                    title.setTextColor(Color.parseColor("#000000"));

                }
            }
        };
        chatRecycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.Adapter adapter, View v, int position) {
                index = position;
                adapter.notifyDataSetChanged();
                TreeClassEntity treeClassEntity = classEntities.get(position);
                PIFragment fragment = new PIFragment(treeClassEntity.getId());
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.chat_fragment, fragment);
                EventBus.getDefault().postSticky(treeClassEntity);
                transaction.commit();
            }
        });
    }
}